//
//  KRSportTrack.m
//  KRSport
//
//  Created by KaoRny on 17/4/22.
//  Copyright © 2017年 KaoRny. All rights reserved.
//

#import "KRSportTrack.h"



@implementation KRSportTrack
{
    //记录的每一段折线的起点
    CLLocation  *_source;
}
//- (MAPolyline *)addPolyklineWithSourceCoordinate:(CLLocationCoordinate2D)sourceCoordinate andDestCoordinate:(CLLocationCoordinate2D)destCoordinate
//{
//    //构造折线数据对象
//    CLLocationCoordinate2D commonPolylineCoords[2];
//    commonPolylineCoords[0].latitude = sourceCoordinate.latitude;
//    commonPolylineCoords[0].longitude = sourceCoordinate.longitude;
//    
//    commonPolylineCoords[1].latitude = destCoordinate.latitude;
//    commonPolylineCoords[1].longitude = destCoordinate.longitude;
//    
//    //构造折线对象
//    MAPolyline *commonPolyline = [MAPolyline polylineWithCoordinates:commonPolylineCoords count:2];
//    
//    return commonPolyline;
//}

- (KRSportPolyLine *)addPolyklineWithDest:(CLLocation *)dest
{
    //判断运动状态
    if (self.sportState != KRSportStateContinue) {
        //暂停,结束运动时,停止生成折线
        //清空记录的折线起点
        _source = nil;
        return nil;
    }
    //判断是否移动 速度=0   当手机在室内时,speed为负值,表明室内GPS被干扰,速度无效
    if (dest.speed <= 0) {
        
        return nil;
    }
    
    //排除已经"过期"的定位数据  计算当前时间和定位时间戳的差值  2秒以内的数据可用
    if ([[NSDate date] timeIntervalSinceDate:dest.timestamp] >= 2) {
        
        return nil;
    }
    //判断首次调用
    if (_source == nil)
    {
        //记录起点
        _source = dest;
        //生成起点大头针模型
        _startAnnotation = [MAPointAnnotation new];
        _startAnnotation.coordinate = dest.coordinate;
        return nil;
    }
    //创建折线
    KRSportPolyLine *polyline = [KRSportPolyLine polylineWithSourceLocation:_source andDestLocation:dest];
    
    //记录下一段的起点
    _source = dest;
    
    return polyline;
}

- (instancetype)initWithSportType:(KRSportType)sportType withSportState:(KRSportState)sportState
{
    if (self == [super init]) {
        //记录运动类型
        _sportType = sportType;
        //设置运动类型图片
        //判断运动类型
        NSString *sportTypeImgName;
        switch (_sportType) {
            case KRSportTypeRunning:
                sportTypeImgName = @"map_annotation_run";
                break;
            case KRSportTypeRiding:
                sportTypeImgName = @"map_annotation_bike";
                break;
            case KRSportTypeWalking:
                sportTypeImgName = @"map_annotation_walk";
                break;
            default:
                break;
        }
        _sportTypeImageName = sportTypeImgName;
        //记录运动状态
        _sportState = sportState;
    }
    return self;
}

@end
